package com.li.shanlin.baseUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author liyinlong
 *
 *         2017年3月9日下午5:46:09
 */
public class BaseUtils {
	/**
	 * 基本调用接口方法
	 * 
	 * @param url
	 * @param fileUrl
	 */
	public static String faceBase(String url, File targetFile) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String str = null;
		try {
			HttpPost httppost = new HttpPost(url);
			httppost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			if ((targetFile!=null)&&(targetFile.isFile()&&targetFile.exists())) {
				FileBody bin = new FileBody(targetFile);
				StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
				HttpEntity reqEntity1 = MultipartEntityBuilder.create().addPart("image", bin)
						.addPart("comment", comment).build();
				httppost.setEntity(reqEntity1);
			}
			CloseableHttpResponse response = httpclient.execute(httppost);

			try {
				System.out.println("----------------------------------------");
				System.out.println(response.getStatusLine());
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					System.out
							.println("Response content length: " + resEntity.getContentLength() + resEntity.toString());

					InputStream instreams = resEntity.getContent();
					str = convertStreamToString(instreams);

				}
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	/**
	 * 基本调用接口方法
	 * 
	 * @param url
	 * @param fileUrl
	 */
	public static String faceBaseComparison(String url, File image_ref1, File image_ref2, File image_ref3,
			File image_best, File image) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String str = null;
		try {
			HttpPost httppost = new HttpPost(url);
			StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
			if ((image!=null)&&(image.isFile()&&image.exists())) {
				FileBody bin = new FileBody(image);

				HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("image", bin).addPart("comment", comment)
						.build();
				httppost.setEntity(reqEntity);
			}
			if ((image_ref1!=null)&&(image_ref1.isFile()&&image_ref1.exists())) {
				FileBody bin = new FileBody(image_ref1);

				HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("image_ref1", bin)
						.addPart("comment", comment).build();
				httppost.setEntity(reqEntity);
			}
			if ((image_ref2!=null)&&(image_ref2.isFile()&&image_ref2.exists())) {
				FileBody bin = new FileBody(image_ref2);

				HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("image_ref2", bin)
						.addPart("comment", comment).build();
				httppost.setEntity(reqEntity);
			}
			if ((image_ref3!=null)&&(image_ref3.isFile()&&image_ref3.exists())) {
				FileBody bin = new FileBody(image_ref3);

				HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("image_ref3", bin)
						.addPart("comment", comment).build();
				httppost.setEntity(reqEntity);
			}
			if ((image_best!=null)&&(image_best.isFile()&&image_best.exists())) {
				FileBody bin = new FileBody(image_best);

				HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("image_best", bin)
						.addPart("comment", comment).build();
				httppost.setEntity(reqEntity);
			}
			CloseableHttpResponse response = httpclient.execute(httppost);

			try {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					System.out
							.println("Response content length: " + resEntity.getContentLength() + resEntity.toString());

					InputStream instreams = resEntity.getContent();
					str = convertStreamToString(instreams);

				}
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	/**
	 * 流转String
	 * 
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			System.out.println("Error=" + e.toString());
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
